package com.ruyuan.eshop.construction.api.impl;

import com.alibaba.fastjson.JSON;
import com.ruyuan.eshop.common.core.JsonResult;
import com.ruyuan.eshop.common.core.PageResult;
import com.ruyuan.eshop.construction.api.ProducingAreaApi;
import com.ruyuan.eshop.construction.domain.dto.ProducingAreaDTO;
import com.ruyuan.eshop.construction.domain.dto.ProducingAreaResultDTO;
import com.ruyuan.eshop.construction.domain.request.QueryProducingAreaRequest;
import com.ruyuan.eshop.construction.domain.request.ProducingAreaRequest;
import com.ruyuan.eshop.common.exception.ProductBizException;
import com.ruyuan.eshop.construction.service.ProducingAreaService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 产地服务
 *
 * @author zhonghuashishan
 */
@Slf4j
@DubboService(version = "1.0.0", interfaceClass = ProducingAreaApi.class, retries = 0)
public class ProducingAreaApiImpl implements ProducingAreaApi {

    @Autowired
    private ProducingAreaService producingAreaService;


    @Override
    public JsonResult<ProducingAreaResultDTO> producingArea(ProducingAreaRequest request) {
        try {
            ProducingAreaResultDTO producingAreaResultDTO = producingAreaService.saveProducingArea(request);
            return JsonResult.buildSuccess(producingAreaResultDTO);
        } catch (ProductBizException e) {
            log.error("biz error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    @Override
    public JsonResult<PageResult<ProducingAreaDTO>> getProducingArea(QueryProducingAreaRequest request) {
        try {
            PageResult<ProducingAreaDTO> producingAreaPageResult = producingAreaService.getProducingArea(request);
            return JsonResult.buildSuccess(producingAreaPageResult);
        } catch (ProductBizException e) {
            log.error("biz error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }
}
